package com.webapp.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.webapp.common.AppException;
import com.webapp.common.ErrorCode;
import com.webapp.mapper.PrdRecommAgentDetailMapper;
import com.webapp.mapper.ProductComprehensiveScoreMapper;
import com.webapp.model.*;
import com.webapp.model.dto.QuestionnaireInfoDto;
import com.webapp.model.vo.FamilyPrdVo;
import com.webapp.model.vo.GongsiQudao;
import com.webapp.model.vo.QuestionnaireInfoVo;
import com.webapp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: Jimmy Li
 * @E-mail: jimmy.c.li@cn.pwc.com
 * @Date: 2020/9/11, 5:16 PM
 * @Description:
 */
@Slf4j
@Service
public class CalculationModelServiceImpl implements CalculationModelService {


    @Autowired
    AgentDetailService agentDetailService;
    @Autowired
    FixedRecommendModelService fixedRecommendModelService;
    @Autowired
    PrdRecommProductDetailService prdRecommProductDetailService;
    @Autowired
    PrdRecommCustomerDetailService prdRecommCustomerDetailService;
    @Autowired
    ProductComprehensiveScoreService productComprehensiveScoreService;//产品综合得分表
    @Autowired
    OneyearAndLonginsuranceNumberService oneyearAndLonginsuranceNumberService;//一年期和长险主推产品数量配置表
    @Autowired
    EtlSerive etlSerive;
    @Autowired
    ScienceGuaranteeConfigService scienceGuaranteeConfigService;
    @Autowired
    PrdRecommAgentProductDetailService prdRecommAgentProductDetailService;

    @Autowired
    TanOrKexueService tanOrKexueService;
    @Autowired
    JieguoService jieguoService;
    @Autowired
    ProductComprehensiveScoreMapper productComprehensiveScoreMapper;
    @Autowired
    PrdRecommAgentDetailMapper prdRecommAgentDetailMapper;


    @Value("${python_url}")
    String python_url;

    /**
     * @return
     */

    @Override
    public QuestionnaireInfoVo getbenren(QuestionnaireInfoDto questionnaireInfoDto) {
        GongsiQudao selectgongsiandqudao = prdRecommAgentDetailMapper.selectgongsiandqudao(questionnaireInfoDto.getAgentCode());


        String gongsi=null;
        String qudao=null;
        if (selectgongsiandqudao==null){
            log.info("代理人无分公司和渠道");
        }else {
            if (selectgongsiandqudao.getBranchId()!=null||!selectgongsiandqudao.getBranchId().equals("")){
                gongsi=selectgongsiandqudao.getBranchId();
            }
            if (selectgongsiandqudao.getAgentCate()!=null||!selectgongsiandqudao.getAgentCate().equals("")){
                qudao=selectgongsiandqudao.getAgentCate();
            }

        }




        String gongsiandqudao=null;
        if ((gongsi!=null&&!gongsi.equals(""))&&(qudao!=null&&!qudao.equals(""))){
            gongsiandqudao=gongsi+"|"+qudao;
        }


        QuestionnaireInfoVo questionnaireInfoVo = new QuestionnaireInfoVo();
        ArrayList<Long> productldsList = new ArrayList<>();//产品id的list
        //请求序列号
        questionnaireInfoVo.setSerialNumber(questionnaireInfoDto.getSerialNumber());

        if (questionnaireInfoDto.getSerialNumber() == null) {

            throw new AppException(ErrorCode.FAILED,"请求序列号不能为空");
        } else if (questionnaireInfoDto.getAgentCode() == null) {
            throw new AppException(ErrorCode.FAILED,"代理人编号不能为空");
        } else if (questionnaireInfoDto.getMainCustomerId() == null) {

            throw new AppException(ErrorCode.FAILED,"发起人客户编号不能为空");
        }
        String agentCode = questionnaireInfoDto.getAgentCode();


        Integer k = null;


        FixedRecommendModel companyAndKey = fixedRecommendModelService.getOne(
                new QueryWrapper<FixedRecommendModel>().eq("companyAndKey", gongsiandqudao));


        ArrayList<Long> longs = new ArrayList<>();
        if (companyAndKey!=null) {
            if (companyAndKey.getProduct1() != null) {
                longs.add(companyAndKey.getProduct1());
            }
            if (companyAndKey.getProduct2() != null) {
                longs.add(companyAndKey.getProduct2());
            }
            if (companyAndKey.getProduct3() != null) {
                longs.add(companyAndKey.getProduct3());
            }
        }

        if (gongsiandqudao==null||longs.size()==0){
            //没有分公司和渠道或者固定推荐模板数量为0 k=0;
            //没有固定推荐模板
            ArrayList<Long> longs31 = new ArrayList<>();

            TanOrKexue tanOrKexue = tanOrKexueService.list().get(0);
            Integer tanOrKexue1 = tanOrKexue.getTanOrKexue();
            ///判断客户类型：//纯赠险客户/老客
            log.info("固定推荐模板数量不是3或固定推荐模板数量为空,判断客户是纯赠险客户/老客,客户编号为：" + questionnaireInfoDto.getMainCustomerId());
            String maincustomerid = questionnaireInfoDto.getMainCustomerId();
            PrdRecommCustomerDetail customerDetail = prdRecommCustomerDetailService.getOne(
                    new QueryWrapper<PrdRecommCustomerDetail>()
                            .eq("cust_puid", maincustomerid)
            .eq("agt_id",agentCode));
            if (customerDetail == null) {

                FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                ArrayList<String> strings = new ArrayList<>();
                strings.add("长险");
                ArrayList<String> strings1 = new ArrayList<>();
                strings1.add("短险");
                strings1.add("一年期");
                ArrayList<Long> list = new ArrayList<>();
                ArrayList<Long> tanxingmoban = prdRecommProductDetailService.tanxingmoban(gongsi, qudao, strings, 2);
                ArrayList<Long> tanxingmoban1 = prdRecommProductDetailService.tanxingmoban(gongsi, qudao, strings1, 1);
                if (tanxingmoban.size()!=0&&tanxingmoban.get(0)!=null){
                    list.add(tanxingmoban.get(0));
                }
                if (tanxingmoban.size()>1&&tanxingmoban.get(1)!=null){
                    list.add(tanxingmoban.get(1));
                }
                if (tanxingmoban1.size()!=0&&tanxingmoban1.get(0)!=null){
                    list.add(tanxingmoban1.get(0));
                }
                if (list.size()<3){
                    ArrayList<Long> xinke = xinke();
                    familyPrdVo.setProductId(xinke);
                }else {
                    familyPrdVo.setProductId(list);
                }


                ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();

                familyPrdVo.setRelation("本人");

                familyPrdVos.add(familyPrdVo);
                questionnaireInfoVo.setProductIds(familyPrdVos);
                return questionnaireInfoVo;
            }
            if (customerDetail.getIfPurezx().equals("1")&&tanOrKexue1==0) {
                //questionnaireInfoDto.getTanOrkexue()==0是弹性模板
                log.info("纯赠险客户,客户编号为：" + maincustomerid+"走的弹性模板");
                //弹性模板开始
                if (k==null){
                    k=0;
                }
                OneyearAndLonginsuranceNumber fixed_model_number = oneyearAndLonginsuranceNumberService.getOne(
                        new QueryWrapper<OneyearAndLonginsuranceNumber>().eq("fixed_model_number", k));
                if (fixed_model_number == null) {
                    log.info("一年期和长险主推产品数量配置表不能为空");
                    throw new AppException(ErrorCode.FAILED,"一年期和长险主推产品数量配置表不能为空" );
                }

                ArrayList<String> statuss1 = new ArrayList<>();
                statuss1.add("短险");
                statuss1.add("一年期");

                FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                //这是短险和一年期
                if ((gongsi==null|| gongsi.equals(""))||(qudao==null||qudao.equals(""))){
                    ArrayList<Long> xinke = xinke();
                    familyPrdVo.setProductId(xinke);

                }


                ArrayList<Long> list1 = prdRecommProductDetailService.tanxingmoban(gongsi, qudao, statuss1, 1);
                //这是短险
                ArrayList<String> strings = new ArrayList<>();
                strings.add("长险");
                ArrayList<Long> list2 = prdRecommProductDetailService.tanxingmoban(gongsi, qudao,strings,2 );

                if (list1.size()!=0&&list1.get(0)!=null){
                    longs31.add(list1.get(0));
                }
                if (list2.size()!=0&&list2.get(0)!=null){
                    longs31.add(list2.get(0));
                }
                if (list2.size()>1&&list2.get(1)!=null){
                    longs31.add(list2.get(1));
                }


                ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();

                familyPrdVo.setRelation("本人");
                if (longs31.size()<3){
                    ArrayList<Long> xinke = xinke();
                    familyPrdVo.setProductId(xinke);
                }else {
                    familyPrdVo.setProductId(longs31);
                }

                familyPrdVos.add(familyPrdVo);
                questionnaireInfoVo.setProductIds(familyPrdVos);
                return questionnaireInfoVo;
                    //弹性模板结束
            }else if (customerDetail.getIfPurezx().equals("1")&&tanOrKexue1==1){
                //科学配置开始
                List<String> strings = getkexuepeizhiClasss(customerDetail.getAge());
                    String s1 = strings.get(0);
                    String s2 = strings.get(1);
                    String s3 = strings.get(2);


                Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                Long oksaleproductId2= productComprehensiveScoreMapper.selectProid(s2, agentCode);
                Long oksaleproductId3 = productComprehensiveScoreMapper.selectProid(s3, agentCode);

                if (oksaleproductId1!=null){
                    longs31.add(oksaleproductId1);
                }
                if (oksaleproductId2!=null){
                    longs31.add(oksaleproductId2);
                }
                if (oksaleproductId3!=null){
                    longs31.add(oksaleproductId3);
                }





                ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                    FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                    familyPrdVo.setRelation("本人");
                if (longs31.size()<3){
                    ArrayList<Long> xinke = xinke();
                    familyPrdVo.setProductId(xinke);
                }else {

                    familyPrdVo.setProductId(longs31);
                }

                    familyPrdVos.add(familyPrdVo);
                    questionnaireInfoVo.setProductIds(familyPrdVos);
                    return questionnaireInfoVo;

            }
            else if (customerDetail.getIfPurezx().equals("0")){
                //老客开始了
                log.info("老客,客户编号为：" + maincustomerid);

                if ((customerDetail.getIfValidSx()==null||!customerDetail.getIfValidSx()) &&
                        (customerDetail.getIfValidYl()==null||!customerDetail.getIfValidYl() )&&
                        (customerDetail.getIfValidYw()==null||!customerDetail.getIfValidYw()) &&
                        (customerDetail.getIfValidZj()==null||!customerDetail.getIfValidZj()) &&
                        (customerDetail.getIfValidNj()==null||!customerDetail.getIfValidNj())) {
                    if (customerDetail.getRecentInvalid()==null){
                        //最近失效保单类型为空
                        Integer age = customerDetail.getAge();
                        List<String> strings1 = getkexuepeizhiClasss(age);//科学配置优先级顺序
                        String typeclass1 = strings1.get(0);
                        String typeclass2 = strings1.get(1);
                        String typeclass3 = strings1.get(2);


                        Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(typeclass1, agentCode);
                        Long oksaleproductId2= productComprehensiveScoreMapper.selectProid(typeclass2, agentCode);
                        Long oksaleproductId3 = productComprehensiveScoreMapper.selectProid(typeclass3, agentCode);


                        if (oksaleproductId1!=null){
                            longs31.add(oksaleproductId1);
                        }
                        if (oksaleproductId2!=null){
                            longs31.add(oksaleproductId2);
                        }
                        if (oksaleproductId3!=null){
                            longs31.add(oksaleproductId3);
                        }

                        ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                        FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                        familyPrdVo.setRelation("本人");
                        if (longs31.size()<3){
                            ArrayList<Long> xinke = xinke();
                            familyPrdVo.setProductId(xinke);
                        }else {
                            familyPrdVo.setProductId(longs31);
                        }

                        familyPrdVos.add(familyPrdVo);
                        questionnaireInfoVo.setProductIds(familyPrdVos);
                        return questionnaireInfoVo;
                    }
                    String recentInvalid = customerDetail.getRecentInvalid();//最近失效的一张保单所属保障类型

                    String statusList1=recentInvalid;




                    log.info("老客,无有效的一年期及以上保单,最近失效的一张保单所属保障类型为：" + recentInvalid);
                    //读取10产品综合得分表（1最近失效保障类型*1产品/保障类型=1产品   +   3-k-1保障类型*1产品/保障类型=3-k-1产品）
                    Integer age = customerDetail.getAge();
                    List<String> strings1 = getkexuepeizhiClasss(age);//科学配置优先级顺序

                    if (k==null){
                        k=0;
                    }
                    Integer n = 3 - k;

                    Long oksaleproductId = productComprehensiveScoreMapper.selectProid(recentInvalid, agentCode);


                    log.info("固定推荐模板数量K=0 "  + ", 则还需要推荐：3种," + "推荐的产品保障类型为(最近失效)： " + recentInvalid);

                    if (oksaleproductId!=null){
                        longs31.add(oksaleproductId);
                    }


                        if (statusList1.equals(strings1.get(0))){

                            Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(strings1.get(1), agentCode);
                            Long oksaleproductId2= productComprehensiveScoreMapper.selectProid(strings1.get(2), agentCode);


                            if (oksaleproductId1!=null){
                                longs31.add(oksaleproductId1);
                            }
                            if (oksaleproductId2!=null){
                                longs31.add(oksaleproductId2);
                            }


                        }else if (statusList1.equals(strings1.get(1))){

                            Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(strings1.get(0), agentCode);
                            Long oksaleproductId2= productComprehensiveScoreMapper.selectProid(strings1.get(2), agentCode);

                            if (oksaleproductId1!=null){
                                longs31.add(oksaleproductId1);
                            }
                            if (oksaleproductId2!=null){
                                longs31.add(oksaleproductId2);
                            }

                        }else if(statusList1.equals(strings1.get(2))){

                            Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(strings1.get(0), agentCode);
                            Long oksaleproductId2= productComprehensiveScoreMapper.selectProid(strings1.get(1), agentCode);

                            if (oksaleproductId1!=null){
                                longs31.add(oksaleproductId1);
                            }
                            if (oksaleproductId2!=null){
                                longs31.add(oksaleproductId2);
                            }


                        }else {

                            Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(strings1.get(0), agentCode);
                            Long oksaleproductId2= productComprehensiveScoreMapper.selectProid(strings1.get(1), agentCode);
                            if (oksaleproductId1!=null){
                                longs31.add(oksaleproductId1);
                            }
                            if (oksaleproductId2!=null){
                                longs31.add(oksaleproductId2);
                            }

                        }
                        //questionnaireInfoVo.setProductIds(longs3);
                        ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                        FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                        familyPrdVo.setRelation("本人");
                    if (longs31.size()<3){
                        ArrayList<Long> xinke = xinke();
                        familyPrdVo.setProductId(xinke);
                    }else {
                        familyPrdVo.setProductId(longs31);
                    }

                        familyPrdVos.add(familyPrdVo);
                        questionnaireInfoVo.setProductIds(familyPrdVos);
                        return questionnaireInfoVo;
                        //至此结束



                } else {
                    log.info("老客,有有效的一年期及有效保单");
                    if (customerDetail.getIfValidSx() && customerDetail.getIfValidYl() && customerDetail.getIfValidYw()
                            && customerDetail.getIfValidZj() && customerDetail.getIfValidNj()) {
                        log.info("老客,当前一年期及以上有效保障类型数量：5种");
                        //读取11科学保障配置建议表（3-k保障类型）
                        Integer age = customerDetail.getAge();
                        List<String> strings1 = getkexuepeizhiClasss(age);//科学配置优先级顺序
                        log.info("科学保障配置建议,保障大类顺序为：" + strings1);
                            String s1 = strings1.get(0);
                            String s2 = strings1.get(1);
                            String s3 = strings1.get(2);

                            log.info("固定推荐模板数量K= " + k + ", 则还需要推荐：3种," + "推荐的产品保障类型为：" + s1 + "," + s2 + "," + s3);


                        Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                        Long oksaleproductId2= productComprehensiveScoreMapper.selectProid(s2, agentCode);
                        Long oksaleproductId3= productComprehensiveScoreMapper.selectProid(s3, agentCode);

                        if (oksaleproductId1!=null){
                            longs31.add(oksaleproductId1);
                        }
                        if (oksaleproductId2!=null){
                            longs31.add(oksaleproductId2);
                        }
                        if (oksaleproductId3!=null){
                            longs31.add(oksaleproductId3);
                        }
                            ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                            FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                            familyPrdVo.setRelation("本人");
                        if (longs31.size()<3){
                            ArrayList<Long> xinke = xinke();
                            familyPrdVo.setProductId(xinke);
                        }else {
                            familyPrdVo.setProductId(longs31);
                        }

                            familyPrdVos.add(familyPrdVo);
                            questionnaireInfoVo.setProductIds(familyPrdVos);
                            return questionnaireInfoVo;


                    } else {
                        log.info("老客,当前一年期及以上有效保障类型数量：1至4");

                        if (customerDetail.getRecentInvalid()==null){
                            ArrayList<String> youxiaoList = new ArrayList<>();
                            if (customerDetail.getIfValidNj()) {
                                youxiaoList.add("年金");
                            }
                            if (customerDetail.getIfValidZj()) {
                                youxiaoList.add("疾病");
                            }
                            if (customerDetail.getIfValidYw()) {
                                youxiaoList.add("意外");
                            }
                            if (customerDetail.getIfValidYl()) {
                                youxiaoList.add("医疗");
                            }
                            if (customerDetail.getIfValidSx()) {
                                youxiaoList.add("寿险");
                            }

                            //最近失效保单类型为空,不一致
                            Integer age = customerDetail.getAge();
                            ArrayList<String> kexueList = getkexuepeizhiClasss(age);//科学配置优先级顺序
                            List<String> strings1 = classList(youxiaoList, kexueList);
                            String typeclass1 = strings1.get(0);
                            String typeclass2 = strings1.get(1);
                            String typeclass3 = strings1.get(2);


                            Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(typeclass1, agentCode);
                            Long oksaleproductId2= productComprehensiveScoreMapper.selectProid(typeclass2, agentCode);
                            Long oksaleproductId3= productComprehensiveScoreMapper.selectProid(typeclass3, agentCode);


                            if (oksaleproductId1!=null){
                                longs31.add(oksaleproductId1);
                            }
                            if (oksaleproductId2!=null){
                                longs31.add(oksaleproductId2);
                            }
                            if (oksaleproductId3!=null){
                                longs31.add(oksaleproductId3);
                            }


                            ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                            FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                            familyPrdVo.setRelation("本人");
                            if (longs31.size()<3){
                                ArrayList<Long> xinke = xinke();
                                familyPrdVo.setProductId(xinke);
                            }else {
                                familyPrdVo.setProductId(longs31);
                            }

                            familyPrdVos.add(familyPrdVo);
                            questionnaireInfoVo.setProductIds(familyPrdVos);
                            return questionnaireInfoVo;

                        }

                        String recentInvalid = customerDetail.getRecentInvalid();//最经失效爆单类型
                        String statusList1=recentInvalid;


                        ArrayList<String> strings = new ArrayList<>();
                        if (customerDetail.getIfValidNj()) {
                            strings.add("年金");
                        }
                        if (customerDetail.getIfValidZj()) {
                            strings.add("疾病");
                        }
                        if (customerDetail.getIfValidYw()) {
                            strings.add("意外");
                        }
                        if (customerDetail.getIfValidYl()) {
                            strings.add("医疗");
                        }
                        if (customerDetail.getIfValidSx()) {
                            strings.add("寿险");
                        }
                        if (strings.contains(statusList1)) {
                            log.info("最近失效保单类型:" + recentInvalid + ",与当前有效的一年期及以上保单保障类型一致");
                            //读取11科学保障配置建议表（3-k保障类型）
                            Integer age = customerDetail.getAge();
                            ArrayList<String> strings1 = getkexuepeizhiClasss(age);
                            //strings1.add(0,statusList1);
                            List<String> strings2 = classList(strings, strings1);

                            log.info("科学保障配置建议,保障大类顺序为：" + strings1);
                                String s1 = strings2.get(0);
                                String s2 = strings2.get(1);
                                String s3 = strings2.get(2);

                                log.info("固定推荐模板数量K= " + k + ", 则还需要推荐：3种," + "推荐的产品保障类型为：" + s1 + "," + s2 + "," + s3);

                            Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                            Long oksaleproductId2= productComprehensiveScoreMapper.selectProid(s2, agentCode);
                            Long oksaleproductId3= productComprehensiveScoreMapper.selectProid(s3, agentCode);


                            if (oksaleproductId1!=null){
                                longs31.add(oksaleproductId1);
                            }
                            if (oksaleproductId2!=null){
                                longs31.add(oksaleproductId2);
                            }
                            if (oksaleproductId3!=null){
                                longs31.add(oksaleproductId3);
                            }

                                //
                                ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                                FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                                familyPrdVo.setRelation("本人");
                            if (longs31.size()<3){
                                ArrayList<Long> xinke = xinke();
                                familyPrdVo.setProductId(xinke);
                            }else {
                                familyPrdVo.setProductId(longs31);
                            }

                                familyPrdVos.add(familyPrdVo);
                                questionnaireInfoVo.setProductIds(familyPrdVos);
                                return questionnaireInfoVo;

                        } else {
                            //读取14客户产品信息统计表（最近失效保单的保障类型1种）
                            //读取11科学保障配置建议表2中

                            String statusList2=recentInvalid;


                            log.info("最近失效保单类型:" + recentInvalid + ",与当前有效的一年期及以上保单保障类型不一致");
                            //读取14客户产品信息统计表（最近失效保单的保障类型1种）读取11科学保障配置建议表（3-k-1保障类型
                            Integer age = customerDetail.getAge();
                            List<String> strings1 = getkexuepeizhiClasss(age);
                            strings1.add(0,statusList2);
                            ArrayList<String> collect = (ArrayList<String>) strings1.stream().distinct().collect(Collectors.toList());
                            List<String> strings5 = classList(strings, collect);


                            String s1 = strings5.get(0);
                            String s2 = strings5.get(1);
                            String s3 = strings5.get(2);


                            Long oksaleproductId = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                            Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(s2, agentCode);
                            Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(s3, agentCode);


                            if (oksaleproductId!=null){
                                longs31.add(oksaleproductId);
                            }
                            if (oksaleproductId1!=null){
                                longs31.add(oksaleproductId1);
                            }
                            if (oksaleproductId2!=null){
                                longs31.add(oksaleproductId2);
                            }

                            ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                            FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                            familyPrdVo.setRelation("本人");
                            if (longs31.size()<3){
                                ArrayList<Long> xinke = xinke();
                                familyPrdVo.setProductId(xinke);
                            }else {
                                familyPrdVo.setProductId(longs31);
                            }

                                familyPrdVos.add(familyPrdVo);
                                questionnaireInfoVo.setProductIds(familyPrdVos);
                                return questionnaireInfoVo;

                        }
                    }
                }
            }


        }else {
            //有分公司和渠道，判断固定推荐模板数量
            ArrayList<Long> longs3 = new ArrayList<>();//固定推荐模板类别
            if (companyAndKey != null) {
                ArrayList<Long> longs11 = new ArrayList<>();
                if (companyAndKey.getProduct1()!=null){
                    longs11.add(companyAndKey.getProduct1());
                    longs3.add(companyAndKey.getProduct1());
                } if (companyAndKey.getProduct2()!=null){
                    longs11.add(companyAndKey.getProduct2());
                    longs3.add(companyAndKey.getProduct2());
                } if (companyAndKey.getProduct3()!=null){
                    longs11.add(companyAndKey.getProduct3());
                    longs3.add(companyAndKey.getProduct3());
                }



                int size = longs11.size();
                log.info("固定推荐模板数量为："+size);
                k=size;
                if (k == 3) {
                    Long product1 = companyAndKey.getProduct1();
                    Long product2 = companyAndKey.getProduct2();
                    Long product3 = companyAndKey.getProduct3();

                    //推荐01固定推荐模板中的3个产品d

                    log.info("主推产品1编号： " + product1);
                    log.info("主推产品2编号： " + product2);
                    log.info("主推产品3编号： " + product3);
                    FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                    familyPrdVo.setRelation("本人");
                    productldsList.add(product1);
                    productldsList.add(product2);
                    productldsList.add(product3);
                    familyPrdVo.setProductId(productldsList);
                    familyPrdVo.setProductId(productldsList);
                    ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                    familyPrdVos.add(familyPrdVo);
                    questionnaireInfoVo.setProductIds(familyPrdVos);
                    return questionnaireInfoVo;
                    //推荐01固定推荐模板中的3个产品结束；
                } else {


                    TanOrKexue tanOrKexue = tanOrKexueService.list().get(0);
                    Integer tanOrKexue1 = tanOrKexue.getTanOrKexue();
                    ///判断客户类型：//纯赠险客户/老客
                    log.info("固定推荐模板数量不是3或固定推荐模板数量为空,判断客户是纯赠险客户/老客,客户编号为：" + questionnaireInfoDto.getMainCustomerId());
                    String maincustomerid = questionnaireInfoDto.getMainCustomerId().toString();
                    PrdRecommCustomerDetail customerDetail = prdRecommCustomerDetailService.getOne(
                            new QueryWrapper<PrdRecommCustomerDetail>().eq("cust_puid", maincustomerid)
                                    .eq("agt_id",agentCode));

                    if (customerDetail == null) {
                        ArrayList<Long> list = new ArrayList<>();
                        if (longs!=null||longs.size()!=0){
                            list.addAll(longs);
                        }
                        ArrayList<Long> xinke = xinke();
                        ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                        FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                        familyPrdVo.setRelation("本人");
                        if (list.size()!=0){
                            list.addAll(xinke);
                        }
                        familyPrdVo.setProductId(list);
                        familyPrdVos.add(familyPrdVo);
                        questionnaireInfoVo.setProductIds(familyPrdVos);
                        return questionnaireInfoVo;
                    } else if (customerDetail.getIfPurezx() == null) {
                        throw new AppException(ErrorCode.FAILED,"客户信息有误,纯赠险/老客信息为空,客户id编号为：" + maincustomerid);
                    }

                    if (customerDetail.getIfPurezx()!=null&&!customerDetail.getIfPurezx().equals("1")&&tanOrKexue1==0) {
                        //questionnaireInfoDto.getTanOrkexue()==0是弹性模板
                        log.info("纯赠险客户,客户编号为：" + maincustomerid+"走的弹性模板");
                        //弹性模板开始
                        OneyearAndLonginsuranceNumber fixed_model_number = oneyearAndLonginsuranceNumberService.getOne(
                                new QueryWrapper<OneyearAndLonginsuranceNumber>().eq("fixed_model_number", k));
                        if (fixed_model_number == null) {
                            log.info("一年期和长险主推产品数量配置表不能为空");
                            throw new AppException(ErrorCode.FAILED,"一年期和长险主推产品数量配置表不能为空" );
                        }

                        Integer oneyearNumber = fixed_model_number.getOneyearNumber();//一年期产品推荐数量
                        Integer longinsurance = fixed_model_number.getLonginsurance();//长险产品推荐数量
                        if (oneyearNumber + longinsurance > 3) {
                            log.info("一年期和长险主推产品数量配置表信息错误,一年期产品推荐数量为：" + oneyearNumber + ", 长险产品推荐数量为：" + longinsurance);
                            throw new AppException(ErrorCode.FAILED,"一年期和长险主推产品数量配置表信息错误,一年期产品推荐数量为："+oneyearNumber+", 长险产品推荐数量为：" + longinsurance);
                        }
                        log.info("弹性模板，固定模板数量为：" + k + ",一年期产品推荐数量:" + oneyearNumber + ",长险产品推荐数量为：" + longinsurance);
                        //n款近7天销量累计最高的短险或一年期产品
                        ArrayList<String> statuss1 = new ArrayList<>();
                        statuss1.add("短险");
                        statuss1.add("一年期");
                        ArrayList<String> strings = new ArrayList<>();
                        strings.add("长险");

                        ArrayList<Long> list1 = prdRecommProductDetailService.tanxingmoban(gongsi, qudao, statuss1, longinsurance);
                        //这是短险
                        ArrayList<Long> list2 = prdRecommProductDetailService.tanxingmoban(gongsi, qudao,strings,oneyearNumber );
                        if (list1.size()==longinsurance&&list2.size()==oneyearNumber){
                            longs11.addAll(list1);
                            longs11.addAll(list2);
                            ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();

                            FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                            familyPrdVo.setRelation("本人");
                            familyPrdVo.setProductId(longs11);
                            familyPrdVos.add(familyPrdVo);
                            questionnaireInfoVo.setProductIds(familyPrdVos);
                            return questionnaireInfoVo;
                        }else {
                            ArrayList<Long> xinke = xinke();
                            longs11.addAll(xinke);
                            ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();

                            FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                            familyPrdVo.setRelation("本人");
                            familyPrdVo.setProductId(longs11);
                            familyPrdVos.add(familyPrdVo);
                            questionnaireInfoVo.setProductIds(familyPrdVos);
                            return questionnaireInfoVo;



                        }

                    }else if (customerDetail.getIfPurezx().equals("1")&&tanOrKexue1==1){
                        //科学配置开始
                        List<String> strings = getkexuepeizhiClasss(customerDetail.getAge());
                        if (k==null){
                            k=0;
                        }
                        Integer n=3-k;
                        if (n==3){
                            String s1 = strings.get(0);
                            String s2 = strings.get(1);
                            String s3 = strings.get(2);

                            Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                            Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(s2, agentCode);
                            Long oksaleproductId3 = productComprehensiveScoreMapper.selectProid(s3, agentCode);


                            longs3.add(oksaleproductId1);
                            longs3.add(oksaleproductId2);
                            longs3.add(oksaleproductId3);

                            ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                            FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                            familyPrdVo.setRelation("本人");
                            if (longs3.size()<3){
                                ArrayList<Long> xinke = xinke();
                                familyPrdVo.setProductId(xinke);
                            }else {
                                familyPrdVo.setProductId(longs3);
                            }

                            familyPrdVos.add(familyPrdVo);
                            questionnaireInfoVo.setProductIds(familyPrdVos);

                            return questionnaireInfoVo;
                        }else if (n==2){
                            String s1 = strings.get(0);
                            String s2 = strings.get(1);
                            String s3 = strings.get(2);



                            Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                            Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(s2, agentCode);
                            Long oksaleproductId3 = productComprehensiveScoreMapper.selectProid(s3, agentCode);

                            if (oksaleproductId1!=null){
                                longs3.add(oksaleproductId1);
                            }
                            if (oksaleproductId2!=null){
                                longs3.add(oksaleproductId2);
                            }
                            if (oksaleproductId3!=null){
                                longs3.add(oksaleproductId3);
                            }

                            List<Long> collect = longs3.stream().distinct().collect(Collectors.toList());


                            ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                            FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                            familyPrdVo.setRelation("本人");
                            if (collect.size()<3){
                                ArrayList<Long> xinke = xinke();
                                familyPrdVo.setProductId(xinke);
                            }else {
                                familyPrdVo.setProductId(collect);
                            }


                            familyPrdVos.add(familyPrdVo);
                            questionnaireInfoVo.setProductIds(familyPrdVos);
                            return questionnaireInfoVo;
                        }else if (n==1){
                            String s1 = strings.get(0);
                            String s2 = strings.get(1);

                            Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                            Long oksaleproductId3 = productComprehensiveScoreMapper.selectProid(s2, agentCode);

                            if (oksaleproductId2!=null){
                                longs3.add(oksaleproductId2);
                            }
                            if (oksaleproductId3!=null){
                                longs3.add(oksaleproductId3);
                            }
                            List<Long> collect = longs3.stream().distinct().collect(Collectors.toList());
                            ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                            FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                            familyPrdVo.setRelation("本人");
                            if (collect.size()<3){
                                ArrayList<Long> xinke = xinke();
                                familyPrdVo.setProductId(xinke);
                            }else {
                                familyPrdVo.setProductId(collect);
                            }

                            familyPrdVos.add(familyPrdVo);
                            questionnaireInfoVo.setProductIds(familyPrdVos);
                            return questionnaireInfoVo;
                        }



                    } else {

//
                        //老客开始了
                        log.info("老客,客户编号为：" + maincustomerid);
                        if (
                                (customerDetail.getIfValidYl()==null||!customerDetail.getIfValidYl())&&
                                (customerDetail.getIfValidYw()==null||!customerDetail.getIfValidYw()) &&
                                (customerDetail.getIfValidZj()==null||!customerDetail.getIfValidZj()) &&
                                (customerDetail.getIfValidNj()==null||!customerDetail.getIfValidNj())) {

                            if (customerDetail.getRecentInvalid()==null){
                                //最近失效保单类型为空
                                Integer age = customerDetail.getAge();
                                List<String> strings1 = getkexuepeizhiClasss(age);//科学配置优先级顺序
                                String typeclass1 = strings1.get(0);
                                String typeclass2 = strings1.get(1);
                                String typeclass3 = strings1.get(2);

                                Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(typeclass1, agentCode);
                                Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(typeclass2, agentCode);
                                Long oksaleproductId3 = productComprehensiveScoreMapper.selectProid(typeclass3, agentCode);

                                if (k==1&&(oksaleproductId1!=null&&oksaleproductId2!=null)){
                                    longs3.add(oksaleproductId1);
                                    longs3.add(oksaleproductId2);
                                }else if (k==2&&oksaleproductId1!=null){
                                    longs3.add(oksaleproductId1);
                                }else if (k==0||k==null){
                                    if (oksaleproductId1!=null){
                                        longs3.add(oksaleproductId1);
                                    }
                                    if (oksaleproductId2!=null){
                                        longs3.add(oksaleproductId2);
                                    }
                                    if (oksaleproductId3!=null){
                                        longs3.add(oksaleproductId3);
                                    }


                                }



                                ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                                FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                                familyPrdVo.setRelation("本人");
                                if (longs3.size()<3){
                                    ArrayList<Long> xinke = xinke();
                                    familyPrdVo.setProductId(xinke);
                                }else {
                                    familyPrdVo.setProductId(longs3);
                                }
                                familyPrdVos.add(familyPrdVo);
                                questionnaireInfoVo.setProductIds(familyPrdVos);
                                return questionnaireInfoVo;

                            }
                            String recentInvalid = customerDetail.getRecentInvalid();//最近失效的一张保单所属保障类型

                            String statusList1=recentInvalid;

                            log.info("老客,无有效的一年期及以上保单,最近失效的一张保单所属保障类型为：" + recentInvalid);
                            //读取10产品综合得分表（1最近失效保障类型*1产品/保障类型=1产品   +   3-k-1保障类型*1产品/保障类型=3-k-1产品）
                            Integer age = customerDetail.getAge();
                            List<String> strings1 = getkexuepeizhiClasss(age);//科学配置优先级顺序


                            Integer n = 3 - k;
                            Long oksaleproductId = productComprehensiveScoreMapper.selectProid(recentInvalid, agentCode);


                            if (n == 1) {
                                log.info("固定推荐模板数量K= " + k + ", 则还需要推荐：1种," + "推荐的产品保障类型为(最近失效)： " + recentInvalid);
                                if (oksaleproductId!=null){
                                    longs11.add(oksaleproductId);
                                }
                                String s = strings1.get(0);
                                Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(s, agentCode);
                                if (oksaleproductId1!=null){
                                    longs11.add(oksaleproductId1);
                                }
                                List<Long> collect = longs11.stream().distinct().collect(Collectors.toList());

                                ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                                FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                                familyPrdVo.setRelation("本人");
                                if (collect.size()<3){
                                    ArrayList<Long> xinke = xinke();
                                    familyPrdVo.setProductId(xinke);
                                }else {
                                    familyPrdVo.setProductId(collect);
                                }

                                familyPrdVos.add(familyPrdVo);
                                questionnaireInfoVo.setProductIds(familyPrdVos);
                                return questionnaireInfoVo;
                            } else if (n == 2) {
                                log.info("固定推荐模板数量K= " + k + ", 则还需要推荐：2种," + "推荐的产品保障类型为(最近失效)： " + recentInvalid);


                                longs11.add(oksaleproductId);
                                if (statusList1.equals(strings1.get(0))){
                                    String s = strings1.get(1);
                                    Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(s, agentCode);
                                    if (oksaleproductId1!=null){
                                        longs11.add(oksaleproductId1);
                                    }
                                    String s1 = strings1.get(2);
                                    Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                                    if (oksaleproductId2!=null){
                                        longs11.add(oksaleproductId2);
                                    }

                                }else{
                                    String s = strings1.get(0);
                                    Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(s, agentCode);
                                    if (oksaleproductId1!=null){
                                        longs11.add(oksaleproductId1);
                                    }
                                    String s1 = strings1.get(1);
                                    Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                                    if (oksaleproductId2!=null){
                                        longs11.add(oksaleproductId2);
                                    }


                                }


                                ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                                FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                                familyPrdVo.setRelation("本人");
                                if (longs11.size()<3){
                                    ArrayList<Long> xinke = xinke();
                                    familyPrdVo.setProductId(xinke);
                                }else {
                                    familyPrdVo.setProductId(longs11);
                                }

                                familyPrdVos.add(familyPrdVo);
                                questionnaireInfoVo.setProductIds(familyPrdVos);
                                return questionnaireInfoVo;
                            } else if (n == 3) {
                                log.info("固定推荐模板数量K= " + k + ", 则还需要推荐：3种," + "推荐的产品保障类型为(最近失效)： " + recentInvalid);

                                longs11.add(oksaleproductId);
                                if (statusList1.equals(strings1.get(0))){
                                    Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(strings1.get(1), agentCode);
                                    Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(strings1.get(2), agentCode);
                                    if (oksaleproductId1!=null){
                                        longs11.add(oksaleproductId1);
                                    }
                                    if (oksaleproductId2!=null){
                                        longs11.add(oksaleproductId2);
                                    }



                                }else if (statusList1.equals(strings1.get(1))){

                                    Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(strings1.get(0), agentCode);
                                    Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(strings1.get(2), agentCode);


                                    if (oksaleproductId1!=null){
                                        longs11.add(oksaleproductId1);
                                    }
                                    if (oksaleproductId2!=null){
                                        longs11.add(oksaleproductId2);
                                    }
                                }else if(statusList1.equals(strings1.get(2))){

                                    Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(strings1.get(0), agentCode);
                                    Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(strings1.get(1), agentCode);

                                    if (oksaleproductId1!=null){
                                        longs11.add(oksaleproductId1);
                                    }
                                    if (oksaleproductId2!=null){
                                        longs11.add(oksaleproductId2);
                                    }

                                }else {

                                    Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(strings1.get(0), agentCode);
                                    Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(strings1.get(1), agentCode);


                                    if (oksaleproductId1!=null){
                                        longs11.add(oksaleproductId1);
                                    }
                                    if (oksaleproductId2!=null){
                                        longs11.add(oksaleproductId2);
                                    }


                                }
                                //questionnaireInfoVo.setProductIds(longs3);
                                ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                                FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                                familyPrdVo.setRelation("本人");
                                if (longs11.size()<3){
                                    ArrayList<Long> xinke = xinke();
                                    familyPrdVo.setProductId(xinke);
                                }else {
                                    familyPrdVo.setProductId(longs11);
                                }


                                familyPrdVos.add(familyPrdVo);
                                questionnaireInfoVo.setProductIds(familyPrdVos);
                                return questionnaireInfoVo;
                                //至此结束
                            }


                        } else {
                            log.info("老客,有有效的一年期及有效保单");
                            if (customerDetail.getIfValidSx() && customerDetail.getIfValidYl() && customerDetail.getIfValidYw()
                                    && customerDetail.getIfValidZj() && customerDetail.getIfValidNj()) {
                                log.info("老客,当前一年期及以上有效保障类型数量：5种");
                                //读取11科学保障配置建议表（3-k保障类型）
                                Integer age = customerDetail.getAge();

                                List<String> strings1 = getkexuepeizhiClasss(age);//科学配置优先级顺序

                                log.info("科学保障配置建议,保障大类顺序为：" + strings1);




                                Integer n = 3 - k;

                                if (n == 1) {
                                    String s1 = strings1.get(0);

                                    log.info("固定推荐模板数量K= " + k + ", 则还需要推荐：1种," + "推荐的产品保障类型为：" +s1);
                                    Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(s1, agentCode);

                                    if (oksaleproductId1!=null){
                                        longs11.add(oksaleproductId1);
                                    }

                                    //questionnaireInfoVo.setProductIds(longs11);
                                    ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                                    FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                                    familyPrdVo.setRelation("本人");
                                    if (longs11.size()<3){
                                        ArrayList<Long> xinke = xinke();
                                        familyPrdVo.setProductId(xinke);
                                    }else {
                                        familyPrdVo.setProductId(longs11);
                                    }

                                    familyPrdVos.add(familyPrdVo);
                                    questionnaireInfoVo.setProductIds(familyPrdVos);
                                    return questionnaireInfoVo;
                                } else if (n == 2) {
                                    String s1 = strings1.get(0);
                                    String s2 = strings1.get(1);



                                    log.info("固定推荐模板数量K= " + k + ", 则还需要推荐：2种," + "推荐的产品保障类型为：" + s1 + "," + s2);
                                    Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(s1, agentCode);

                                    Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(s2, agentCode);

                                    if (oksaleproductId1!=null){
                                        longs11.add(oksaleproductId1);
                                    }
                                    if (oksaleproductId2!=null){
                                        longs11.add(oksaleproductId2);
                                    }

                                    ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                                    FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                                    familyPrdVo.setRelation("本人");
                                    if (longs11.size()<3){
                                        ArrayList<Long> xinke = xinke();
                                        familyPrdVo.setProductId(xinke);
                                    }else {
                                        familyPrdVo.setProductId(longs11);
                                    }

                                    familyPrdVos.add(familyPrdVo);
                                    questionnaireInfoVo.setProductIds(familyPrdVos);
                                    return questionnaireInfoVo;
                                } else if (n == 3) {
                                    String s1 = strings1.get(0);
                                    String s2 = strings1.get(1);
                                    String s3 = strings1.get(2);

                                    log.info("固定推荐模板数量K= " + k + ", 则还需要推荐：3种," + "推荐的产品保障类型为：" + s1 + "," + s2 + "," + s3);

                                    Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                                    Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(s2, agentCode);
                                    Long oksaleproductId3 = productComprehensiveScoreMapper.selectProid(s3, agentCode);


                                    if (oksaleproductId1!=null){
                                        longs11.add(oksaleproductId1);
                                    }
                                    if (oksaleproductId2!=null){
                                        longs11.add(oksaleproductId2);
                                    }
                                    if (oksaleproductId3!=null){
                                        longs11.add(oksaleproductId3);
                                    }


                                    //questionnaireInfoVo.setProductIds(longs3);
                                    ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                                    FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                                    familyPrdVo.setRelation("本人");
                                    if (longs11.size()<3){
                                        ArrayList<Long> xinke = xinke();
                                        familyPrdVo.setProductId(xinke);
                                    }else {
                                        familyPrdVo.setProductId(longs11);
                                    }

                                    familyPrdVos.add(familyPrdVo);
                                    questionnaireInfoVo.setProductIds(familyPrdVos);
                                    return questionnaireInfoVo;
                                }

                            } else {
                                log.info("老客,当前一年期及以上有效保障类型数量：1至4");

                                if (customerDetail.getRecentInvalid()==null){
                                    //最近失效保单类型为空
                                    Integer age = customerDetail.getAge();
                                    ArrayList<String> strings1 = getkexuepeizhiClasss(age);//科学配置优先级顺序
                                    ArrayList<String> strings = new ArrayList<>();
                                    if (customerDetail.getIfValidNj()) {
                                        strings.add("年金");
                                    }
                                    if (customerDetail.getIfValidZj()) {
                                        strings.add("疾病");
                                    }
                                    if (customerDetail.getIfValidYw()) {
                                        strings.add("意外");
                                    }
                                    if (customerDetail.getIfValidYl()) {
                                        strings.add("医疗");
                                    }
                                    if (customerDetail.getIfValidSx()) {
                                        strings.add("寿险");
                                    }
                                    List<String> strings2 = classList(strings, strings1);


                                    String typeclass1 = strings2.get(0);
                                    String typeclass2 = strings2.get(1);
                                    String typeclass3 = strings2.get(2);



                                    Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(typeclass1, agentCode);
                                    Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(typeclass2, agentCode);
                                    Long oksaleproductId3 = productComprehensiveScoreMapper.selectProid(typeclass3, agentCode);

                                    if (oksaleproductId1!=null){
                                        longs11.add(oksaleproductId1);
                                    }
                                    if (oksaleproductId2!=null){
                                        longs11.add(oksaleproductId2);
                                    }
                                    if (oksaleproductId3!=null){
                                        longs11.add(oksaleproductId3);
                                    }
                                    List<Long> collect = longs11.stream().distinct().collect(Collectors.toList());

                                    ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                                    FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                                    familyPrdVo.setRelation("本人");
                                    if (collect.size()<3){
                                        ArrayList<Long> xinke = xinke();
                                        familyPrdVo.setProductId(xinke);
                                    }else {
                                        familyPrdVo.setProductId(collect);
                                    }

                                    familyPrdVos.add(familyPrdVo);
                                    questionnaireInfoVo.setProductIds(familyPrdVos);
                                    return questionnaireInfoVo;

                                }
                                String recentInvalid = customerDetail.getRecentInvalid();//最经失效爆单类型
                                String statusList1=recentInvalid;

                                ArrayList<String> strings = new ArrayList<>();
                                if (customerDetail.getIfValidNj()) {
                                    strings.add("年金");
                                }
                                if (customerDetail.getIfValidZj()) {
                                    strings.add("疾病");
                                }
                                if (customerDetail.getIfValidYw()) {
                                    strings.add("意外");
                                }
                                if (customerDetail.getIfValidYl()) {
                                    strings.add("医疗");
                                }
                                if (customerDetail.getIfValidSx()) {
                                    strings.add("寿险");
                                }
                                if (strings.contains(statusList1)) {
                                    log.info("最近失效保单类型:" + recentInvalid + ",与当前有效的一年期及以上保单保障类型一致");
                                    //读取11科学保障配置建议表（3-k保障类型）
                                    Integer age = customerDetail.getAge();
                                    List<String> strings2 = getkexuepeizhiClasss(age);

                                    strings2.add(0,statusList1);
                                    ArrayList<String> kexueList = (ArrayList<String>) strings2.stream().distinct().collect(Collectors.toList());
                                    List<String> strings1 = classList(strings, kexueList);


                                    log.info("科学保障配置建议,保障大类顺序为：" + strings1);
                                    Integer n = 3 - k;
                                    if (n == 1) {
                                        String s = strings1.get(0);
                                        String s1 = strings1.get(1);
                                        String s2 = strings1.get(2);


                                        log.info("固定推荐模板数量K= " + k + ", 则还需要推荐：1种," + "推荐的产品保障类型为：" + s);

                                        Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(s, agentCode);
                                        Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                                        Long oksaleproductId3 = productComprehensiveScoreMapper.selectProid(s2, agentCode);

                                        if (oksaleproductId1!=null){
                                            longs11.add(oksaleproductId1);
                                        }
                                        if (oksaleproductId2!=null){
                                            longs11.add(oksaleproductId2);
                                        }
                                        if (oksaleproductId3!=null){
                                            longs11.add(oksaleproductId3);
                                        }
                                        List<Long> collect = longs11.stream().distinct().collect(Collectors.toList());


                                        ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                                        FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                                        familyPrdVo.setRelation("本人");
                                        if (collect.size()<3){
                                            ArrayList<Long> xinke = xinke();
                                            familyPrdVo.setProductId(xinke);
                                        }else {
                                            familyPrdVo.setProductId(collect);
                                        }

                                        familyPrdVos.add(familyPrdVo);
                                        questionnaireInfoVo.setProductIds(familyPrdVos);
                                        return questionnaireInfoVo;
                                    } else if (n == 2) {
                                        String s1 = strings1.get(0);
                                        String s2 = strings1.get(1);
                                        String s3 = strings1.get(2);

                                        Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                                        Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(s2, agentCode);
                                        Long oksaleproductId3 = productComprehensiveScoreMapper.selectProid(s3, agentCode);

                                        if (oksaleproductId1!=null){
                                            longs11.add(oksaleproductId1);
                                        }
                                        if (oksaleproductId2!=null){
                                            longs11.add(oksaleproductId2);
                                        }
                                        if (oksaleproductId3!=null){
                                            longs11.add(oksaleproductId3);
                                        }
                                        List<Long> collect = longs11.stream().distinct().collect(Collectors.toList());


                                        ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                                        FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                                        familyPrdVo.setRelation("本人");
                                        if (collect.size()<3){
                                            ArrayList<Long> xinke = xinke();
                                            familyPrdVo.setProductId(xinke);
                                        }else {
                                            familyPrdVo.setProductId(collect);
                                        }

                                        familyPrdVos.add(familyPrdVo);
                                        questionnaireInfoVo.setProductIds(familyPrdVos);
                                        return questionnaireInfoVo;
                                    } else if (n == 3) {
                                        String s1 = strings1.get(0);
                                        String s2 = strings1.get(1);
                                        String s3 = strings1.get(2);

                                        log.info("固定推荐模板数量K= " + k + ", 则还需要推荐：3种," + "推荐的产品保障类型为：" + s1 + "," + s2 + "," + s3);
                                        Long oksaleproductId1 = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                                        Long oksaleproductId2 = productComprehensiveScoreMapper.selectProid(s2, agentCode);
                                        Long oksaleproductId3 = productComprehensiveScoreMapper.selectProid(s3, agentCode);
                                        if (oksaleproductId1!=null){
                                            longs11.add(oksaleproductId1);
                                        }
                                        if (oksaleproductId2!=null){
                                            longs11.add(oksaleproductId2);
                                        }
                                        if (oksaleproductId3!=null){
                                            longs11.add(oksaleproductId3);
                                        }



                                        ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                                        FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                                        familyPrdVo.setRelation("本人");
                                        if (longs11.size()<3){
                                            ArrayList<Long> xinke = xinke();
                                            familyPrdVo.setProductId(xinke);
                                        }else {
                                            familyPrdVo.setProductId(longs11);
                                        }

                                        familyPrdVos.add(familyPrdVo);
                                        questionnaireInfoVo.setProductIds(familyPrdVos);
                                        return questionnaireInfoVo;
                                    }


                                } else {

                                    ArrayList<String> youxiaoList = new ArrayList<>();
                                    if (customerDetail.getIfValidNj()) {
                                        youxiaoList.add("年金");
                                    }
                                    if (customerDetail.getIfValidZj()) {
                                        youxiaoList.add("疾病");
                                    }
                                    if (customerDetail.getIfValidYw()) {
                                        youxiaoList.add("意外");
                                    }
                                    if (customerDetail.getIfValidYl()) {
                                        youxiaoList.add("医疗");
                                    }
                                    if (customerDetail.getIfValidSx()) {
                                        youxiaoList.add("寿险");
                                    }


                                    String statusList2=recentInvalid;


                                    log.info("最近失效保单类型:" + recentInvalid + ",与当前有效的一年期及以上保单保障类型不一致");
                                    //读取14客户产品信息统计表（最近失效保单的保障类型1种）读取11科学保障配置建议表（3-k-1保障类型
                                    Integer age = customerDetail.getAge();
                                    ArrayList<String> strings1 = getkexuepeizhiClasss(age);
                                    strings1.add(0,statusList2);
                                    strings1.stream().distinct().collect(Collectors.toList());
                                    List<String> strings6 = classList(youxiaoList, strings1);
                                    String s3 = strings6.get(0);

                                    Integer n = 3 - k;


                                    Long oksaleproductId = productComprehensiveScoreMapper.selectProid(s3, agentCode);

                                    if (n == 1) {
                                        log.info("固定推荐模板数量K= " + k + ", 则还需要推荐：1种," + "推荐的产品保障类型为(最近失效)： " + recentInvalid);
                                        if (oksaleproductId!=null){
                                            longs11.add(oksaleproductId);
                                        }
                                        String s = strings6.get(1);
                                        String s1 = strings6.get(2);
                                        Long oksaleproductId7 = productComprehensiveScoreMapper.selectProid(s, agentCode);
                                        if (oksaleproductId7!=null){
                                            longs11.add(oksaleproductId7);
                                        }
                                        Long oksaleproductId8 = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                                        if (oksaleproductId8!=null){
                                            longs11.add(oksaleproductId8);
                                        }

                                        ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                                        FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                                        familyPrdVo.setRelation("本人");
                                        List<Long> collect = longs11.stream().distinct().collect(Collectors.toList());
                                        if (collect.size()<3){
                                            ArrayList<Long> xinke = xinke();
                                            familyPrdVo.setProductId(xinke);
                                        }else {
                                            familyPrdVo.setProductId(collect);
                                        }

                                        familyPrdVos.add(familyPrdVo);
                                        questionnaireInfoVo.setProductIds(familyPrdVos);
                                        return questionnaireInfoVo;

                                    } else if (n == 2) {
                                        log.info("固定推荐模板数量K= " + k + ", 则还需要推荐：2种," + "推荐的产品保障类型为(最近失效)： " + recentInvalid);
                                        longs11.add(oksaleproductId);

                                        String s = strings6.get(1);
                                        String s1 = strings6.get(2);
                                        String s2 = strings6.get(3);

                                        Long oksaleproductId7 = productComprehensiveScoreMapper.selectProid(s, agentCode);
                                        Long oksaleproductId8 = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                                        Long oksaleproductId9 = productComprehensiveScoreMapper.selectProid(s2, agentCode);


                                        if (oksaleproductId7!=null){
                                                longs11.add(oksaleproductId7);
                                        }
                                        if (oksaleproductId8!=null){
                                            longs11.add(oksaleproductId8);
                                        }
                                        if (oksaleproductId9!=null){
                                            longs11.add(oksaleproductId9);
                                        }


                                        List<Long> collect = longs11.stream().distinct().collect(Collectors.toList());
                                        //questionnaireInfoVo.setProductIds(longs11);
                                        ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                                        FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                                        familyPrdVo.setRelation("本人");
                                        if (collect.size()<3){
                                            ArrayList<Long> xinke = xinke();
                                            familyPrdVo.setProductId(xinke);
                                        }else {
                                            familyPrdVo.setProductId(collect);
                                        }

                                        familyPrdVos.add(familyPrdVo);
                                        questionnaireInfoVo.setProductIds(familyPrdVos);
                                        return questionnaireInfoVo;
                                    } else if (n == 3) {
                                        longs11.add(oksaleproductId);

                                            String s1 = strings6.get(1);
                                            String s2 = strings6.get(2);

                                        Long oksaleproductId7 = productComprehensiveScoreMapper.selectProid(s1, agentCode);
                                        Long oksaleproductId8 = productComprehensiveScoreMapper.selectProid(s2, agentCode);

                                            if (oksaleproductId7!=null){
                                                longs11.add(oksaleproductId7);

                                            }
                                            if (oksaleproductId8!=null){
                                                longs11.add(oksaleproductId8);
                                            }


                                            ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
                                        FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                                        familyPrdVo.setRelation("本人");
                                        if (longs11.size()<3){
                                            ArrayList<Long> xinke = xinke();
                                            familyPrdVo.setProductId(xinke);
                                        }else {
                                            familyPrdVo.setProductId(longs11);
                                        }

                                        familyPrdVos.add(familyPrdVo);
                                        questionnaireInfoVo.setProductIds(familyPrdVos);
                                        return questionnaireInfoVo;
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }

        if (companyAndKey==null){
            log.info("未匹配到固定推荐模板,则k=0");
            k=0;
        }

        return questionnaireInfoVo;
    }



    public  ArrayList<String> getkexuepeizhiClasss(Integer age){
        String agerfanwei=null;
        if (age<=18){
            agerfanwei="<=18";
        }else if (age<=40&&age>18){
            agerfanwei="19~40";
        }else if (age<=55&&age>40){
            agerfanwei="41~55";
        }else if (age>=56){
            agerfanwei=">=56";
        }
            List<ScienceGuaranteeConfig> list = scienceGuaranteeConfigService.list(new QueryWrapper<ScienceGuaranteeConfig>()
                    .eq("age_range",agerfanwei));
            ScienceGuaranteeConfig scienceGuaranteeConfig = list.get(0);
            String priority = scienceGuaranteeConfig.getPriority();
            List<String> result =  Arrays.asList(priority.split(">"));
        ArrayList<String> strings = new ArrayList<>();
        strings.add(result.get(0));
        strings.add(result.get(1));
        strings.add(result.get(2));
        strings.add(result.get(3));
        strings.add(result.get(4));

        return strings;
    }

    public  List<String> classList(ArrayList<String> youxiaoList,ArrayList<String> kexueList){

        ArrayList<String> kexueList1 = new ArrayList<>();
        kexueList1.add(kexueList.get(0));
        kexueList1.add(kexueList.get(1));
        kexueList1.add(kexueList.get(2));
        kexueList1.add(kexueList.get(3));
        kexueList1.add(kexueList.get(4));
        if (youxiaoList==null){
            return kexueList1;
        }
        if (youxiaoList.size()==1){
            String s = youxiaoList.get(0);
            Iterator<String> it = kexueList.iterator();
            while (it.hasNext()){
                String next = it.next();
                if (s.equals(next)){
                    it.remove();
                }
            }

        }
        else if (youxiaoList.size()==2){
            String s1 = youxiaoList.get(0);
            String s2 = youxiaoList.get(1);
            kexueList.remove(s1);
            kexueList.remove(s2);

        }
        else if (youxiaoList.size()==3){
            String s1 = youxiaoList.get(0);
            String s2 = youxiaoList.get(1);
            String s3 = youxiaoList.get(2);
            boolean remove = kexueList.remove(s1);
            System.out.println(remove+"!!!!!!!!!!!!!!!!11");
            kexueList.remove(s2);
            kexueList.remove(s3);
            System.out.println(kexueList+"!!!!!!!!!!!!!!!!!!!!!!");
        }
        else if (youxiaoList.size()==4){
            String s1 = youxiaoList.get(0);
            String s2 = youxiaoList.get(1);
            String s3 = youxiaoList.get(2);
            String s4 = youxiaoList.get(3);
            kexueList.remove(s1);
            kexueList.remove(s2);
            kexueList.remove(s3);
            kexueList.remove(s4);


        }
        kexueList.addAll(kexueList1);
        List<String> collect = kexueList.stream().distinct().collect(Collectors.toList());
        return collect;
    }


    @Override
    public  ArrayList<Long> xinke(){
        return prdRecommProductDetailService.getxinkeList();
    }

    @Override
    public ArrayList<Long> doudi(String gongsi, String qudao) {
        return prdRecommProductDetailService.getdoudi(gongsi,qudao);
    }





}
